namespace ScrumSprintMonitor.UI.Wpf.Converters
{
	#region #using Directives

	using System;
	using System.Globalization;
	using System.Windows.Data;
	using System.Windows.Media;

	using Infrastructure;
	using Infrastructure.Configuration;

	using Microsoft.Practices.ServiceLocation;

	#endregion

	public class WorkTimeRatioBrushValueConverter : BaseColorToSolidBrushValueHelper,
	                                                IMultiValueConverter
	{
		#region Implementation of IMultiValueConverter

		private IPresentationConfiguration presentationConfiguration;
		private IRemainingWorkStatusEvaluator remainingWorkStatusEvaluator;

		public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
		{
			var status = RemainingWorkStatus.Normal;
			double ratio = double.NaN;
			Color color;

			if (this.remainingWorkStatusEvaluator == null)
			{
				this.remainingWorkStatusEvaluator = ServiceLocator.Current.GetInstance<IRemainingWorkStatusEvaluator>();
			}
			if (this.presentationConfiguration == null)
			{
				this.presentationConfiguration = ServiceLocator.Current.GetInstance<IPresentationConfiguration>();
			}

			if (values != null && values[0] is double)
			{
				double remainingSprintWorkWithUnassigned = (double) values[0];
				double remainingSprintHours = (double) values[1];
				ratio = remainingSprintWorkWithUnassigned / remainingSprintHours;
				status = this.remainingWorkStatusEvaluator.Evaluate(remainingSprintWorkWithUnassigned, remainingSprintHours);
			}

			if (((string) parameter) == "Foreground")
			{
				var configuration = this.presentationConfiguration.GetRemainingWorkStatusPresentationConfig(status);
				color = configuration.ForegroundColor;
			}
			else
			{
				color = RemainingWorkStatusColorHelper.GetColor(status, ratio, (string) parameter, this.presentationConfiguration);
			}

			if (targetType == typeof(Color))
			{
				return color;
			}

			return GetBrushFromColor(color);
		}

		public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
		{
			throw new NotImplementedException();
		}

		#endregion
	}
}